<?
function translate($key) {
	$text = str_replace('_', ' ', $key);
	return $text;
}

function url($controller, $action = "index") {
	return getBaseUrl() . $controller . '/' . $action;
}

// Creates a hyperlink url based on a static file url
function staticUrl($url) {
	return getBaseUrl() . $url;
}

function resourceUrl($url) {
    return getBaseUrl() . 'application/view/layout/' . $url;
}

function icon($key, $text) {
	return '<img src="' . getBaseUrl() . 'view/template/' . getTemplate() . '/images/icons/' . $key . '.gif" class="icon" align="absmiddle" alt="' . $text . '" title="' . $text . '"/>';
}

function goBack() {
	header("location:" . $_SERVER['HTTP_REFERER']);
	exit ();
}

function redirect($url) {
	header("location:" . getBaseUrl() . $url);
	exit ();
}

function getTitle() {
	return $GLOBALS['configuration']['title'];
}

function getBaseDirectory() {
	return $GLOBALS['configuration']['path'];
}

function getBaseUrl() {
	return $GLOBALS['configuration']['url'];
}

function getViewUrl() {
	return $GLOBALS['configuration']['viewurl'];
}

function getTemplateUrl() {
	return getBaseUrl() . 'view/template/' . getTemplate() . '/';
}

function getTemplate() {
	return $GLOBALS['configuration']['template'];
}

// Parses date in format mm/dd/YY hh and returns an array containing all this values
function parseDate($date) {
	$split = explode(" ", $date);
	$dates = explode("-", $split[0]);
	$times = explode(":", $split[1]);

	$y = $dates[0];
	$m = $dates[1]; 
 	$d = $dates[2];
	
	$h = $times[0];
	$min = $times[1];
	$s = $times[2];
	
	if($h > 24 || $min > 60 || $s > 60 || $h < 0 || $min < 0 || $s < 0){
		return null;
	}

	return mktime($h, $min, $s, $m, $d, $y);
}

function check_email_address($email) {
	// First, we check that there's one @ symbol, and that the lengths are right
	if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
		// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
		return false;
	}
	// Split it into sections to make life easier
	$email_array = explode("@", $email);
	$local_array = explode(".", $email_array[0]);
	for ($i = 0; $i < sizeof($local_array); $i++) {
		if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
			return false;
		}
	}
	if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
		$domain_array = explode(".", $email_array[1]);
		if (sizeof($domain_array) < 2) {
			return false; // Not enough parts to domain
		}
		for ($i = 0; $i < sizeof($domain_array); $i++) {
			if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
				return false;
			}
		}
	}
	return true;
}

function getSearchBar($list, $field, $url) {
	$abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	$html = '<ul class="searchbar">';

	$html .= '<li><a href="' . getBaseUrlIndexed() . $url . '">' . translate("all") . '</a></li>';
	for ($i = 0; $i < strlen($abc); $i++) {
		$link = false;
		for ($a = 0; $a < count($list); $a++) {
			$str = $list[$a]-> $field ();
			if ($str {
				0 }
			== strtolower($abc {
				$i })) {
				$link = true;
				break;
			}
		}
		if ($link) {
			$html .= '<li><a href="' . getBaseUrlIndexed() . $url . 'letter/' . $abc {
				$i }
			 . '">' . $abc {
				$i }
			 . '</a></li>';
		} else {
			$html .= '<li>' . $abc {
				$i }
			 . '</li>';
		}
	}

	$html .= '</ul>';
	return $html;
}
?>